package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.anno.LogOperation;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 16778
 * @date 2025/10/7 16:38
 * @description
 */
@RestController
@Slf4j
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result page(@RequestParam(required = false) String name,
                       @RequestParam(required = false) Integer subject,
                       @RequestParam(required = false) Integer target,
                       Integer page,Integer pageSize){
        PageResult<Course> pageResult = courseService.page(name,subject,target,page,pageSize);
        return Result.success(pageResult);
    }

    @LogOperation
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("需要删除的课程id:{}",id);
        courseService.delete(id);
        return Result.success();
    }

    @LogOperation
    @PostMapping
    public Result insert(@RequestBody Course course){
        log.info("需要插入的课程信息：{}",course);
        courseService.insert(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("需要查询的课程id:{}",id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @LogOperation
    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("需要更新的course:{}",course);
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Course> list = courseService.findAll();
        return Result.success(list);
    }

    @GetMapping("/subject/{subject}")
    public Result selectBySubject(@PathVariable String subject){
        log.info("需要查询的学科:{}",subject);
        List<Course> list = courseService.selectBySubject(subject);
        return Result.success(list);
    }

}
